package com.ptswitch.material.pages.contract;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;

import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.model.contract.ContractItem;
import com.ptswitch.material.model.material.Material;
import com.ptswitch.material.pages.base.MaterialBasePage;
import com.ptswitch.material.service.ContractItemService;
import com.ptswitch.material.service.ContractService;
import com.ptswitch.material.service.MaterialService;
import com.ptswitch.material.util.MaterialUtil;

public class AddItem extends MaterialBasePage {
    @Inject
    private Logger logger;
    @Inject
    private ContractService contractSer;
    @Inject
    private ContractItemService contractItemSer;
    @Inject
    private MaterialService materialSer;
    @Property
    private Contract contract;
    private String contractId;
    @Property
    private Material material;
    private String materialId;
    @Property
    private ContractItem contractItem;
    //
    @InjectPage
    private AddItems addItems;
    @InjectPage
    private ViewContract viewContract;

    Object initialize(String contractId, String materialId) {
        this.contractId = contractId;
        this.materialId = materialId;
        return this;
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(String contractId, String materialId) {
        initialize(contractId, materialId);
        contract = contractSer.findById(contractId);
        checkUserIsEqual(contract.getCreater());
        if (contract.isContractOk()) {
            setMessage("合同已完成");
            return viewContract.initialize(contract.getId());
        }
        material = materialSer.findById(materialId);
        contractItem = MaterialUtil.toContractItem(material);
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return new Object[] { contractId, materialId };
    }

    @SetupRender
    Object display() {
        return null;
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "addItem")
    Object addItem() {
        checkUserIsEqual(contract.getCreater());
        contractItem.setContract(contract);
        //check code
        if (contractItemSer.existContractItemCode(contractItem)) {
            setMessage("合同中已有该物料编码");
            return null;
        }
        contractItem.computePrice();
        contractItemSer.save(contractItem);
        // update total price
        contract = contractSer.findById(contractId, ContractService.Init_Items);
        contract.updateForAddItem(contractItem);
        contractSer.save(contract);
//		//send email
//		EMail email=EMailUtil.newEMail();
//		email.setSubject(contract.getCreater().getRealname()+"添加了新的物料项");
//		email.setTosSplit(contract.getFinancial().getEmail()+";"+contract.getProduction().getEmail());
//		String message=getWebUrl()+"/contract/view/"+contract.getId();
//		email.addContentText(message+"\n");
//		EMailUtil.send(email);
        return addItems.initialize(contractId, "", 50, 1);
    }

}
